package ldk.util.refresh;

/**
 * A simple HeaderView Listener
 * Created by ldkxingzhe@163.com on 2017/6/8.
 */
public class SimpleRefreshListener implements HeaderViewListener{

    private float mCurrentValue;
    private boolean mIsRefreshing = false;

    SimpleRefreshManager mSimpleRefreshManager;
    private RefreshListener mListener;

    public SimpleRefreshListener(RefreshListener listener){
        mListener = listener;
    }

    public interface RefreshListener{
        void onRefresh();
    }

    @Override
    public void showHeaderView(float value) {
        mCurrentValue = value;
    }

    public void setRefreshing(boolean isRefreshing){
        mIsRefreshing = isRefreshing;
        if (mSimpleRefreshManager != null){
            if (mIsRefreshing){
                mSimpleRefreshManager.animateToRefreshing();
            }else{
                mSimpleRefreshManager.animateToOriginal();
            }
        }
    }

    @Override
    public void onTouchEnd() {
        if (Math.abs(mCurrentValue) > 20 && !mIsRefreshing){
            mIsRefreshing = true;
            if (mListener != null) mListener.onRefresh();
        }
    }
}
